package com.luomo.wenyuan.modules.wenyuan.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.luomo.wenyuan.common.utils.PageUtils;
import com.luomo.wenyuan.common.utils.R;
import com.luomo.wenyuan.modules.wenyuan.dao.TPurchaseBookDao;
import com.luomo.wenyuan.modules.wenyuan.entity.TBookEntity;
import com.luomo.wenyuan.modules.wenyuan.entity.TPurchaseApplicationEntity;
import com.luomo.wenyuan.modules.wenyuan.entity.TPurchaseBookEntity;
import com.luomo.wenyuan.modules.wenyuan.service.TBookService;
import com.luomo.wenyuan.modules.wenyuan.service.TOrderBookService;
import com.luomo.wenyuan.modules.wenyuan.service.TPurchaseApplicationService;
import com.luomo.wenyuan.modules.wenyuan.service.TPurchaseBookService;
import com.luomo.wenyuan.modules.wenyuan.vo.TPurchaseApplicationVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TPurchaseBook;
import com.luomo.wenyuan.modules.wenyuan.vo.TPurchaseBookVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TPurchaseCheckVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @author
 * @email
 * @date 2020-10-19 15:58:40
 */
@RestController
@RequestMapping("generator/tpurchaseapplication")
public class TPurchaseApplicationController {
    @Autowired
    private TPurchaseApplicationService tPurchaseApplicationService;

    @Autowired
    private TPurchaseBookService tPurchaseBookService;

    @Autowired
    private TPurchaseBookDao tPurchaseBookDao;

    @Autowired
    private TBookService tBookService;

    @Autowired
    private TOrderBookService tOrderBookService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tPurchaseApplicationService.queryPage(params);
        List<TPurchaseApplicationEntity> list = (List<TPurchaseApplicationEntity>) page.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            List<TPurchaseCheckVo> vos = new ArrayList<>();
            TPurchaseCheckVo vo;
            for (TPurchaseApplicationEntity applicationEntity : list) {
                vo = new TPurchaseCheckVo();
                BeanUtil.copyProperties(applicationEntity, vo);
                List<TPurchaseBookEntity> purchaseBookEntities = tPurchaseBookDao.getByPurchaseId(applicationEntity.getId());
                List<TPurchaseBookVo> bookVos = new ArrayList<>();
                TPurchaseBookVo bookVo;
                for (TPurchaseBookEntity purchaseBookEntity : purchaseBookEntities) {
                    TBookEntity tBookEntity = tBookService.getById(purchaseBookEntity.getBookId());
                    bookVo = new TPurchaseBookVo();
                    BeanUtil.copyProperties(tBookEntity, bookVo);
                    bookVo.setNum(purchaseBookEntity.getNum());
                    bookVos.add(bookVo);
                }
                vo.setBookList(bookVos);
                vos.add(vo);
            }
            page.setList(vos);
        }
        return R.ok().put("page", page);
    }

    @RequestMapping("/list/book")
    public R listBook(@RequestParam Map<String, Object> params) {
        PageUtils page = tPurchaseBookService.queryPage(params);
        List<TPurchaseBookEntity> list = (List<TPurchaseBookEntity>) page.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            List<TPurchaseBook> vos = new ArrayList<>();
            TPurchaseBook vo;
            for (TPurchaseBookEntity entity : list) {
                vo = new TPurchaseBook();
                Integer bookId = entity.getBookId();
                Integer num = entity.getNum();
                Integer purchaseId = entity.getPurchaseId();
                Integer readyNum = tOrderBookService.getBookNumByPurchaseIdAndBookId(purchaseId, bookId);
                readyNum = readyNum == null ? 0 : readyNum;
                if (num - readyNum > 0) {
                    TPurchaseApplicationEntity purchase = tPurchaseApplicationService.getById(purchaseId);
                    vo.setPurchaseId(purchase.getId());
                    vo.setApplicant(purchase.getApplicant());
                    vo.setApplyDate(purchase.getApplyDate());
                    TBookEntity book = tBookService.getById(bookId);
                    vo.setBook(book);
                    vo.setNum(num);
                    vo.setReadyNum(readyNum);
                    vo.setNote(purchase.getNote());
                    vos.add(vo);
                }
            }
            page.setList(vos);
        }
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TPurchaseApplicationEntity tPurchaseApplication = tPurchaseApplicationService.getById(id);

        return R.ok().put("tPurchaseApplication", tPurchaseApplication);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody TPurchaseApplicationVo vo) {
        TPurchaseApplicationEntity applicationEntity = new TPurchaseApplicationEntity();
        applicationEntity.setApplicant(vo.getApplicant());
        applicationEntity.setNote(vo.getNote());
        applicationEntity.setApplyDate(new Date());
        applicationEntity.setStatus(1);
        tPurchaseApplicationService.save(applicationEntity);
        List<TPurchaseBookEntity> bookList = vo.getBookList();
        Integer applicationEntityId = applicationEntity.getId();
        for (TPurchaseBookEntity bookEntity : bookList) {
            bookEntity.setPurchaseId(applicationEntityId);
        }
        tPurchaseBookService.saveBatch(bookList);
        return R.ok();
    }

    @RequestMapping("/check/{id}")
    public R check(@PathVariable("id") int id) {
        TPurchaseApplicationEntity applicationEntity = tPurchaseApplicationService.getById(id);
        if (applicationEntity != null) {
            applicationEntity.setStatus(2);
        }
        tPurchaseApplicationService.updateById(applicationEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TPurchaseApplicationEntity tPurchaseApplication) {
        tPurchaseApplicationService.updateById(tPurchaseApplication);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tPurchaseApplicationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
